﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wicresoft.EKA.NRemedy;

namespace Wicresoft.EKA.MVCExtensions
{
    public class Base : IBase
    {
        private string _userIdentify;
        public string UserIdentify
        {
            get
            {
                return this._userIdentify;
            }
            set
            {
                this._userIdentify = value;
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        { }

        public DateTime GetLastModifyDate(object model, out string UserName)
        {
            var type = model.GetType();
            var formAttribute = type.GetCustomAttributes(false).Where(a => a is ARFormAttribute).FirstOrDefault() as ARFormAttribute;
            if (formAttribute != null)
            {
                string name = formAttribute.FormName;
                if (!string.IsNullOrWhiteSpace(name))
                {
                    var entry = this.GetARServer().GetEntry(name,
                    (string)type.GetProperty("RequestID").GetValue(model), new BMC.ARSystem.FieldIdList() { 5, 6 });
                    UserName = (string)entry[5];
                    if (UserName == System.Configuration.ConfigurationManager.AppSettings["SystemUser"])
                    {
                        return DateTime.MinValue;
                    }
                    else
                    {
                        return (DateTime)entry[6];
                    }
                }
            }
            UserName = string.Empty;
            return DateTime.MinValue;
        }
    }
}
